package com.itstudy.controller;

import com.itstudy.pojo.Dept;
import com.itstudy.pojo.Result;
import com.itstudy.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequestMapping("/depts")  //公共路径抽取
@RestController
@Slf4j
public class DeptController {
//    private static final Logger log = LoggerFactory.getLogger(DeptController.class);
    @Autowired
    private DeptService deptService;
//    @RequestMapping(value = "/depts",method = RequestMethod.GET)

//    查询部门信息
    @GetMapping
    public Result list() {
        log.info("查询全部");
        List<Dept> deptlist =deptService.findAll();
        return Result.success(deptlist);
    }

//    删除部门:使用RequestParam,RequestParam使用时默认需要传递参数，如果有时不需要传递参数，则需要加上required属性，required属性默认为true
//    @DeleteMapping("/depts")
//    public Result delete(@RequestParam("id") Integer deptId){
//        System.out.println("删除部门的id"+deptId);
//        return Result.success();
//    }
//    删除部门：不使用RequestParam
    @DeleteMapping
    public Result delete(Integer id){
//        System.out.println("删除部门的id"+id);
        log.info("删除部门的id{}",id);
        deptService.deleteById(id);
        return Result.success();
    }

//    新增部门
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("新增部门{}",dept);
        deptService.addDept(dept);
        return Result.success();
    }

    /**
     * 根据id查询部门
     */
    @GetMapping("/{id}")
    public  Result findById(@PathVariable("id") Integer deptId){
//        System.out.println("查询部门的id:"+deptId);
        log.info("查询部门的id{}",deptId);
        Dept dept = deptService.getById(deptId);
        return Result.success(dept);
    }

    /**
     * 修改部门
     */
    @PutMapping
    public Result update(@RequestBody Dept dept){
//        System.out.println("修改部门"+dept);
        log.info("修改部门{}",dept);
        deptService.updateDept(dept);
        return Result.success();
    }
}
